<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);
class position extends admin {
	private $db;
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('position_model');
	}
	
	public function init() {
			$infos = array();
			
			
			$deptlist = array();
			$deptdb = pc_base::load_model('dept_model');
			$depres = $deptdb->select("","id,name");
			if($depres)foreach($depres as $v){
				$deptlist[$v['id']] = $v['name'];
			}
			
			//$where = "";
			//$page = $_GET['page'] ? $_GET['page'] : '1';
			//$infos = $this->db->listinfo($where, $order = 'listorder DESC,id DESC', $page, $pagesize = 20);
			//$pages = $this->db->pages;
			
			$result = $this->db->select('','*','','listorder ASC,id ASC');
			
			
			$show_dialog = true;
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=admin&c=position&a=add\', title:\'添加职位\', width:\'500\', height:\'160\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', '添加职位');
 			
			$tree = pc_base::load_sys_class('tree');
			$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			
			$array = array();
			foreach($result as $r) {
				$r['cname'] = ($r['name']);
				$r['dept'] = $deptlist[$r['deptid']];
				$r['str_manage'] = '<a href="javascript:edit('.$r['id'].', \''.new_addslashes($r['name']).'\')">编辑</a> | <a href="javascript:confirmurl(\'?m=admin&c=position&a=delete&id='.$r['id'].'\', \'是否删除？\')">删除</a> ';
				$array[] = $r;
			}
			
			$str  = "<tr>
					<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
					<td align='center'>\$id</td>
					<td >\$spacer\$cname</td>
					<td >\$dept</td>
					<td align='center'>\$str_manage</td>
				</tr>";
			$tree->init($array);
			$categorys = $tree->get_tree(0, $str);
			
			
			
			include $this->admin_tpl('position_list');
	}
	
	/**
	 * 推荐位添加
	 */
	public function add() {
		if(isset($_POST['dosubmit'])) {
			if(!is_array($_POST['info']) || empty($_POST['info']['name'])){
				showmessage(L('operation_failure'));
			}
			$_POST['info']['deptid'] = intval($_POST['info']['deptid']);
			$_POST['info']['data'] = $_POST['info']['data'];
			$insert_id = $this->db->insert($_POST['info'],true);
			//$this->_set_cache();
			if($insert_id){
				showmessage(L('operation_success'), '', '', 'add');
			}
		} else {
			pc_base::load_sys_class('form');
				
			$deptlist = array();
			$deptdb = pc_base::load_model('dept_model');
			$deptlist = $deptdb->select("parentid=0","id,name");
			
			$tree = pc_base::load_sys_class('tree');
			$result = $this->db->select();
			$array = array();
			foreach($result as $r) {
				$r['cname'] = ($r['name']);
				$r['selected'] = $r['id'] == $_GET['parentid'] ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			
			
			$show_header = $show_validator = true;
			include $this->admin_tpl('position_add');
		}
		
	}
	
	/**
	 * 推荐位编辑
	 */
	public function edit() {
		if(isset($_POST['dosubmit'])) {
			$_POST['id'] = intval($_POST['id']);
			if(!is_array($_POST['info']) || empty($_POST['info']['name'])){
				showmessage(L('operation_failure'));
			}
			$_POST['info']['deptid'] = intval($_POST['info']['deptid']);
			$_POST['info']['data'] = trim($_POST['info']['data']);		
			
			$this->db->update($_POST['info'],array('id'=>$_POST['id']));
			//$this->_set_cache();
			showmessage(L('operation_success'), '', '', 'edit');
		} else {
			$id = intval($_POST['id']);
			$info = $this->db->get_one(array('id'=>intval($_GET['id'])));
		
			pc_base::load_sys_class('form');
			
			$deptlist = array();
			$deptdb = pc_base::load_model('dept_model');
			$deptlist = $deptdb->select("parentid=0","id,name");
			
			
			$tree = pc_base::load_sys_class('tree');
			$result = $this->db->select();
			$array = array();
			foreach($result as $r) {
				$r['cname'] = ($r['name']);
				$r['selected'] = $r['id'] == $_GET['parentid'] ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			
			$show_validator = $show_header = $show_scroll = true;
			include $this->admin_tpl('position_edit');
		}

	}
	
	/**
	 * 推荐位删除
	 */
	public function delete() {
		$id = intval($_GET['id']);
		$this->db->delete(array('id'=>$id));
		//$this->_set_cache();
		showmessage(L('posid_del_success'),'?m=admin&c=position');
	}
	
	/**
	 * 推荐位排序
	 */
	public function listorder() {
		if(isset($_POST['dosubmit'])) {
			foreach($_POST['listorders'] as $id => $listorder) {
				$this->db->update(array('listorder'=>$listorder),array('id'=>$id));
			}
			//$this->_set_cache();
			showmessage(L('operation_success'),'?m=admin&c=position');
		} else {
			showmessage(L('operation_failure'),'?m=admin&c=position');
		}
	}
}
?>